﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Communication.Serial
{
    public class SerialCommunication : ISerialCommunication
    {
        #region 事件
        public event ISerialCommunication.DataReceivedCallback DataReceivedEvent;
        #endregion
        private SerialPort serialPort;
        // 端口号
        public string PortName { get; set; } = "COM1";
        // 波特率
        public int BaudRate { get; set; } = 9600;
        public bool IsOpen { get => serialPort.IsOpen; }
        // 返回数据
        private string _ReceivedData;
        public string ReceivedData 
        {
            get=> _ReceivedData;
            set { _ReceivedData = value; DataReceivedEvent?.Invoke(_ReceivedData); }
        }
        public SerialCommunication()
        {
            serialPort = new SerialPort();
            serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
        }

        public SerialCommunication(string portName, int baudRate)
        {
            PortName = portName;
            BaudRate = baudRate;
            serialPort = new SerialPort(portName, baudRate);
            serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
        }

        // 打开端口
        public void OpenPort()
        {
            serialPort.BaudRate = BaudRate;
            serialPort.PortName = PortName;
            try
            {
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                    Console.WriteLine("串口已打开");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"打开串口时出错: {ex.Message}");
            }
        }
        // 发送数据
        public void SendData(string data)
        {
            try
            {
                if (serialPort.IsOpen)
                {
                    serialPort.WriteLine(data);
                    Console.WriteLine($"已发送数据: {data}");
                }
                else
                {
                    Console.WriteLine("串口未打开，无法发送数据");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发送数据时出错: {ex.Message}");
            }
        }

        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            ReceivedData = sp.ReadExisting();
            Console.WriteLine($"接收到数据: {ReceivedData}");
        }
        // 关闭端口
        public void ClosePort()
        {
            try
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                    Console.WriteLine("串口已关闭");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"关闭串口时出错: {ex.Message}");
            }
        }
    }
}
